<template>
  <q-page padding>
    <div class="items-center row q-gutter-md" style="margin-bottom: 10px">
      <q-input
        outlined
        dense
        clearable
        style="width: 15%"
        v-model="queryParams.sqlparam_code"
        :label="t('Sqlparam') + t('Code')"
      />
      <q-input
        outlined
        dense
        clearable
        style="width: 15%"
        v-model="queryParams.sqlparam_name"
        :label="t('Sqlparam') + t('Name')"
      />
      <q-select
        outlined
        dense
        emit-value
        clearable
        map-options
        style="width: 15%"
        v-model="queryParams.sqlparam_type"
        :label="t('Sqlparam') + t('Type')"
        :options="dictOptions.sqlparamType"
      />
      <q-btn color="primary" @click="handleSearch" :label="$t('Search')" />
      <q-btn color="primary" @click="resetSearch" :label="$t('Reset')" />
    </div>
    <q-space />
    <q-table
      dense
      row-key="id"
      separator="cell"
      :rows="tableData"
      :columns="columns"
      v-model:pagination="pagination"
      :rows-per-page-options="pageOptions"
      :loading="loading"
      @request="onRequest"
    >
      <template v-slot:top>
        <q-btn
          color="primary"
          @click="showAddForm()"
          :label="$t('Add') + ' ' + $t('Sqlparam')"
        />
      </template>
      <template v-slot:body-cell-sqlparam_type="props">
        <q-td :props="props">
          <DcDictCell
            dictName="sqlparamType"
            :dictCode="props.row.sqlparam_type"
          />
        </q-td>
      </template>
      <template v-slot:body-cell-created_at="props">
        <q-td :props="props">
          {{ showDateTime(props.row.created_at) }}
        </q-td>
      </template>
      <template v-slot:body-cell-updated_at="props">
        <q-td :props="props">
          {{ showDateTime(props.row.updated_at) }}
        </q-td>
      </template>
      <template v-slot:body-cell-actions="props">
        <q-td :props="props">
          <div class="q-gutter-xs">
            <q-btn
              color="primary"
              size="sm"
              @click="showEditForm(props.row)"
              :label="$t('Edit')"
              :disable="props.row.sqlparam_type === 'internal'"
            />
            <q-btn
              color="negative"
              size="sm"
              @click="handleDelete(props.row)"
              :label="$t('Delete')"
              :disable="props.row.sqlparam_type === 'internal'"
            />
          </div>
        </q-td>
      </template>
      <template v-slot:loading>
        <q-inner-loading
          :showing="loading"
          :label="$t('Loading...')"
          label-class="text-teal"
          label-style="font-size: 1.1em"
        >
        </q-inner-loading>
      </template>
    </q-table>
    <sqlparamRecordDetail
      ref="recordDetailDialog"
      @handleFinish="handleFinish"
    />
  </q-page>
</template>

<script setup>
import useTableData from "src/composables/useTableData";
import { computed, unref, onMounted, ref, toRefs } from "vue";
import { useI18n } from "vue-i18n";
import sqlparamRecordDetail from "./SqlparamRecordDetail.vue";
import { FormatDateTime } from "src/utils/date";

const { t } = useI18n();
const url = {
  list: "sqlparam/get-sqlparam-list",
  delete: "sqlparam/delete-sqlparam-by-id",
};

const columns = computed(() => {
  return [
    {
      name: "sqlparam_code",
      align: "center",
      label: t("Sqlparam") + t("Code"),
      field: "sqlparam_code",
    },
    {
      name: "sqlparam_name",
      align: "center",
      label: t("Sqlparam") + t("Name"),
      field: "sqlparam_name",
    },
    {
      name: "sqlparam_type",
      align: "center",
      label: t("Sqlparam") + t("Type"),
      field: "sqlparam_type",
    },
    {
      name: "default_value",
      align: "center",
      label: t("Default") + t("Value"),
      field: "default_value",
    },
    {
      name: "created_by",
      align: "center",
      label: t("CreatedBy"),
      field: "created_by",
    },
    {
      name: "created_at",
      align: "center",
      label: t("CreatedAt"),
      field: "created_at",
    },
    {
      name: "updated_by",
      align: "center",
      label: t("UpdatedBy"),
      field: "updated_by",
    },
    {
      name: "updated_at",
      align: "center",
      label: t("UpdatedAt"),
      field: "updated_at",
    },
    {
      name: "actions",
      align: "center",
      label: t("Actions"),
      field: "actions",
    },
  ];
});

const {
  DcDictCell,
  dictOptions,
  pagination,
  queryParams,
  pageOptions,
  loading,
  tableData,
  recordDetailDialog,
  onRequest,
  getTableData,
  handleSearch,
  resetSearch,
  showAddForm,
  showEditForm,
  handleFinish,
  handleDelete,
} = useTableData(url);

onMounted(() => {
  getTableData();
});

const showDateTime = computed(() => {
  return (datetime) => {
    return FormatDateTime(datetime);
  };
});
</script>

<script>
import { defineComponent } from "vue";

export default defineComponent({
  name: "SqlparamComp",
});
</script>
